package com.bank.service;

import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.ListIterator;
import java.util.UUID;
import javax.annotation.Resource;
import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bank.domain.Account;
import com.bank.domain.AccountTypes;
import com.bank.domain.Currency;
import com.bank.domain.Customer;
import com.bank.domain.Transaction;
import com.bank.service.AccountService;



@Service("transactionService")
@Transactional
public class TransactionService {

	protected static Logger logger = Logger.getLogger("service");

	@Resource(name="hashingService")
	private HashingService hashingService;
	
	@Resource(name="sessionFactory")
	private SessionFactory sessionFactory;
	
	@Resource(name="customerService")
	private CustomerService customerService;
	
	
	
	
	public Boolean recordTransaction(Transaction transaction) {
		logger.debug("Making transaction");
		// Retrieve session from Hibernate
		Session session = sessionFactory.getCurrentSession();
		session.save(transaction);
		return true;
	}
	
	public Object getTransactions() {
		Session session = sessionFactory.getCurrentSession();
		Query accounts = session.createSQLQuery(
				"SELECT accounts.CREATED_AT, accounts.CURRENCY_ID, accounts.UPDATED_AT, accounts.ACCOUNT_ID, accounts.AccountTypes, accounts.Currency, accounts.ACCOUNT_HASH, accounts.ACCOUNT_IDENTITY, accounts.ACCOUNT_TYPE_ID, accounts.AMOUNT FROM accounts INNER JOIN customeraccount ON accounts.ACCOUNT_ID = customeraccount.ACCOUNT_ID WHERE customeraccount.CUSTOMER_ID =" + customerService.getCurrentId() + " GROUP BY accounts.ACCOUNT_IDENTITY").addEntity("accounts", Account.class);		
		List<Account> USRAccounts = accounts.list();
		Iterator<Account> iterator2 = USRAccounts.iterator();
		String cquery = "";
		String last = "";
		while (iterator2.hasNext()) {
			
			Account Obb = iterator2.next();
			last = Obb.getAccountIdentity();
			cquery = cquery.concat(" OR ACCOUNT_FROM='" + Obb.getAccountIdentity() + "' OR ACCOUNT_TO='" + Obb.getAccountIdentity() + "'");		
		 }		
		
		Query query = session.createSQLQuery(
				"SELECT transactions.TRANSACTION_STATUS_ID, transactions.TRANSACTION_HASH, transactions.TransactionStatuss, transactions.TRANSACTION_ID, transactions.ACCOUNT_FROM, transactions.ACCOUNT_TO, transactions.TRANSACTION_AMOUNT, transactions.TRANSACTION_DATE, transactions.TRANSACTION_DETAILS, customeraccount.CUSTOMER_ID FROM transactions INNER JOIN customeraccount ON transactions.CUSTOMER_ID = customeraccount.CUSTOMER_ID WHERE ACCOUNT_FROM =  '" + last + "'" + cquery + " GROUP BY transactions.TRANSACTION_DATE");
		//List<Transaction> cc = query.list();
		System.out.println("Query is: " +query);
		System.out.println("EXPORT Query is: SELECT transactions.TRANSACTION_STATUS_ID, transactions.TRANSACTION_HASH, transactions.TransactionStatuss, transactions.TRANSACTION_ID, transactions.ACCOUNT_FROM, transactions.ACCOUNT_TO, transactions.TRANSACTION_AMOUNT, transactions.TRANSACTION_DATE, transactions.TRANSACTION_DETAILS, customeraccount.CUSTOMER_ID FROM transactions INNER JOIN customeraccount ON transactions.CUSTOMER_ID = customeraccount.CUSTOMER_ID " + cquery + " GROUP BY transactions.TRANSACTION_DATE");
		try {
			
	    	return query.list();
		} catch (Exception e) {
			return null;
		}		
	
	}	
	

	public List<List<String>> exportTransactions() {
		Session session = sessionFactory.getCurrentSession();
		String last = "";
		Object[] OO = new Object[100];
		List<String> record=new ArrayList<String>();
		Query accounts = session.createSQLQuery(
				"SELECT accounts.CREATED_AT, accounts.CURRENCY_ID, accounts.UPDATED_AT, accounts.ACCOUNT_ID, accounts.AccountTypes, accounts.Currency, accounts.ACCOUNT_HASH, accounts.ACCOUNT_IDENTITY, accounts.ACCOUNT_TYPE_ID, accounts.AMOUNT FROM accounts INNER JOIN customeraccount ON accounts.ACCOUNT_ID = customeraccount.ACCOUNT_ID WHERE customeraccount.CUSTOMER_ID =" + customerService.getCurrentId() + " GROUP BY accounts.ACCOUNT_IDENTITY").addEntity("accounts", Account.class);
		List<Account> USRAccounts = accounts.list();
		Iterator<Account> iterator2 = USRAccounts.iterator();
		String cquery = "";
		while (iterator2.hasNext()) {
			Account Obb = iterator2.next();
			last = Obb.getAccountIdentity();
			cquery = cquery.concat(" OR ACCOUNT_FROM='" + Obb.getAccountIdentity() + "' OR ACCOUNT_TO='" + Obb.getAccountIdentity() + "'");		
		 }
		int i = 0;
		Query query = session.createSQLQuery(
				"SELECT transactions.TRANSACTION_STATUS_ID, transactions.TRANSACTION_HASH, transactions.TransactionStatuss, transactions.TRANSACTION_ID, transactions.ACCOUNT_FROM, transactions.ACCOUNT_TO, transactions.TRANSACTION_AMOUNT, transactions.TRANSACTION_DATE, transactions.TRANSACTION_DETAILS, customeraccount.CUSTOMER_ID FROM transactions INNER JOIN customeraccount ON transactions.CUSTOMER_ID = customeraccount.CUSTOMER_ID WHERE ACCOUNT_FROM =  '" + last + "'" + cquery + " GROUP BY transactions.TRANSACTION_DATE").addEntity("transactions", Transaction.class);
			System.out.println("YoYo: "+query);
			List<Transaction> cc = query.list();
			Iterator<Transaction> iterator = cc.iterator();
			while (iterator.hasNext()) {
				Transaction Ob = iterator.next();
				record.clear();
				record.add(0,Ob.getCustomerId().toString());
				record.add(1,Ob.getTransactionId().toString());
				record.add(2,Ob.getTransactionStatusId().toString());
				record.add(3,Ob.getTransactionHash());
				record.add(4,Ob.getAccountFrom());
				record.add(5,Ob.getAccountTo());
				record.add(6,Ob.getTransactionDetails());
				record.add(7,String.valueOf(Ob.getTransactionAmount()));
				record.add(8,Ob.getTransactionDate().toString());
				OO[i] = "" + record;
				i++;
			}

		int count = 0;
		for(int ii=0; ii < OO.length; ii++){
			if(OO[ii] == null ){
				break;
			}
			count++;
		}
		
		Object[] Res = new Object[count];
		for(int ii=0; ii < count; ii++){
			Res[ii] = OO[ii];
		}
		
		List<List<String>> bb = new ArrayList<List<String>>();
		for(Object Ob: Res){
			String V = Ob.toString();
			String VV = V.replace("[", "").replace("]", "");
			
			List<String> D = Arrays.asList(VV);
			bb.add(D);

		}		
		//System.out.println("SELECT transactions.TRANSACTION_STATUS_ID, transactions.TRANSACTION_HASH, transactions.TransactionStatuss, transactions.TRANSACTION_ID, transactions.ACCOUNT_FROM, transactions.ACCOUNT_TO, transactions.TRANSACTION_AMOUNT, transactions.TRANSACTION_DATE, transactions.TRANSACTION_DETAILS, customeraccount.CUSTOMER_ID FROM transactions INNER JOIN customeraccount ON transactions.CUSTOMER_ID = customeraccount.CUSTOMER_ID " + cquery + " GROUP BY transactions.TRANSACTION_DATE");
		return bb;
	}









}
